//
//  LLSlideInteractor.m
//  LLCoreUserInterface
//
//  Created by my_liu on 2016/11/16.
//  Copyright © 2016年 my_liu. All rights reserved.
//

#import "LLSlideInteractor.h"

@implementation LLSlideInteractor

# pragma mark -
# pragma mark Helpers

#define PresentedViewWidthLandscape [UIScreen mainScreen].bounds.size.width
#define PresentedViewHeightLandscape [UIScreen mainScreen].bounds.size.height
- (CGRect)rectForDismissedState:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIView *containerView = [transitionContext containerView];
    if (self.isDownDissmiss) {
        return CGRectMake(0, containerView.bounds.size.height,PresentedViewWidthLandscape, containerView.bounds.size.height);
    } else {
        return CGRectMake(containerView.bounds.size.width, 0,PresentedViewWidthLandscape, containerView.bounds.size.height);
    }
}
- (CGRect)rectForPresentedState:(id<UIViewControllerContextTransitioning>)transitionContext
{
    if (self.isDownDissmiss) {
        return CGRectOffset([self rectForDismissedState:transitionContext], 0, -PresentedViewHeightLandscape);
    } else {
        return CGRectOffset([self rectForDismissedState:transitionContext], -PresentedViewWidthLandscape, 0);
    }
}
# pragma mark -
# pragma mark UIViewControllerTransitioningDelegate

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.presenting = YES;
    return self;
}


- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.presenting = NO;
    return self;
}


# pragma mark -
# pragma mark UIViewControllerAnimatedTransitioning

- (void)animationEnded:(BOOL)transitionCompleted
{
    // reset state
    self.presenting = NO;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.3f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    
    if (self.presenting) {
        toViewController.view.frame = [self rectForDismissedState:transitionContext];
        toViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [containerView addSubview:toViewController.view];
        
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            toViewController.view.frame = [self rectForPresentedState:transitionContext];
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    } else {
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            fromViewController.view.frame = [self rectForDismissedState:transitionContext];
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
            [fromViewController.view removeFromSuperview];
        }];
    }
}

@end
